<template>
	<view class="contr-container">
		<!-- header -->
		<t-header @back="clickNavBackFn">线上合同</t-header>
		<!-- content -->
		<view class="contr-content">
			<view class="contr-content-tabs">
				<view class="contr-content-tab">合同详情</view>
			</view>
			<view class="contr-content-comp">
				<scroll-view class="page" v-if="moban">
					<view class="top-logo">
						<image :src="moban.logo" mode="heightFix"></image>
					</view>
				
					<view class="tit">
						{{moban.Title}}
					</view>
					<view class="sub-tit">
						{{moban.SubTitle}}
					</view>
					<view class="ab-info" v-if="con_info.typedata == 1">
						<view class="info-item">
							<text>甲方：{{moban.JiaName}}</text>
						</view>
						<view class="info-item">
							<text>地址：{{moban.JiaAddress}}</text>
						</view>
						<view class="info-item">
							<text>乙方：{{con_info.name}}</text>
						</view>
						<view class="info-item">
							<text>地址：{{con_info.address}}</text>
						</view>
						<view class="info-item">
							<text>身份证号：{{con_info.idnumber}}</text>
						</view>
					</view>
					<view class="content">
						<u-parse :content="moban.Content"></u-parse>
					</view>
				
					<view class="sign-info">
						<view class="sign-item sign-left">
							<view class="sign-pic">
								<image :src="moban.Pic" mode="widthFix"></image>
							</view>
							<view class="item-row">
								<text>甲方(盖章)</text>
							</view>
							<view class="item-row">
								<text>甲方代表(签字)</text>
								<image :src="moban.JiaSign" mode="widthFix"></image>
							</view>
							<view class="item-row">
								<text>{{con_info.signDate}}</text>
							</view>
						</view>
						<view class="sign-item sign-right">
							<view class="item-row">
								<text>乙方：{{con_info.name}}</text>
							</view>
							<view class="item-row">
								<text>乙方代表(签字)</text>
								<image v-if="con_info.signimage != ''" :src="con_info.signimage" mode="widthFix"></image>
							</view>
							<view class="item-row">
								<text>{{con_info.signDate}}</text>
							</view>
						</view>
					</view>
					<view class="sign-box" v-if="con_info.tgdata == 1">
						<view class="sign-box-box">
							<image v-if="sign_img_temp!=''" :src="sign_img_temp" mode="widthFix"></image>
							 <view v-if="sign_img_temp == ''" class="down-sign" @tap="showSign = true">
								点击进行横屏签字
							</view>
						</view>
				
						<view class="sign-box-buttons">
							<tui-button type="blue" shape="circle" width="9rem" height="2.6rem"
								@tap="clearSign">重新签名</tui-button>
							<tui-button type="danger" shape="circle" width="9rem" height="2.6rem" @tap="doSign">确认提交</tui-button>
						</view>
					</view>
				</scroll-view>
					<sign-board v-if="showSign" @close="close" @savesign="savesign"></sign-board>
			</view>
		</view>
	</view>
	
	<!-- <view>
		<scroll-view class="page" v-if="moban">
			<view class="top-logo">
				<image :src="moban.logo" mode="heightFix"></image>
			</view>

			<view class="tit">
				{{moban.Title}}
			</view>
			<view class="sub-tit">
				{{moban.SubTitle}}
			</view>
			<view class="ab-info" v-if="con_info.typedata == 1">
				<view class="info-item">
					<text>甲方：{{moban.JiaName}}</text>
				</view>
				<view class="info-item">
					<text>地址：{{moban.JiaAddress}}</text>
				</view>
				<view class="info-item">
					<text>乙方：{{con_info.name}}</text>
				</view>
				<view class="info-item">
					<text>地址：{{con_info.address}}</text>
				</view>
				<view class="info-item">
					<text>身份证号：{{con_info.idnumber}}</text>
				</view>
			</view>
			<view class="content">
				<u-parse :content="moban.Content"></u-parse>
			</view>

			<view class="sign-info">
				<view class="sign-item sign-left">
					<view class="sign-pic">
						<image :src="moban.Pic" mode="widthFix"></image>
					</view>
					<view class="item-row">
						<text>甲方(盖章)</text>
					</view>
					<view class="item-row">
						<text>甲方代表(签字)</text>
						<image :src="moban.JiaSign" mode="widthFix"></image>
					</view>
					<view class="item-row">
						<text>{{con_info.signDate}}</text>
					</view>
				</view>
				<view class="sign-item sign-right">
					<view class="item-row">
						<text>乙方：{{con_info.name}}</text>
					</view>
					<view class="item-row">
						<text>乙方代表(签字)</text>
						<image v-if="con_info.signimage != ''" :src="con_info.signimage" mode="widthFix"></image>
					</view>
					<view class="item-row">
						<text>{{con_info.signDate}}</text>
					</view>
				</view>
			</view>
			<view class="sign-box" v-if="con_info.tgdata == 1">
				<view class="sign-box-box">
					<image v-if="sign_img_temp!=''" :src="sign_img_temp" mode="widthFix"></image>
					 <view v-if="sign_img_temp == ''" class="down-sign" @tap="showSign = true">
						点击进行横屏签字
					</view>
				</view>

				<view class="sign-box-buttons">
					<tui-button type="blue" shape="circle" width="9rem" height="2.6rem"
						@tap="clearSign">重新签名</tui-button>
					<tui-button type="danger" shape="circle" width="9rem" height="2.6rem" @tap="doSign">确认提交</tui-button>
				</view>
			</view>
		</scroll-view>
			<sign-board v-if="showSign" @close="close" @savesign="savesign"></sign-board>

	</view> -->
</template>

<script>
	import {
		contractDetail,
		one,
		two,
		doSign
	} from '@/api/user.js'
	import uParse from '@/components/uni/uParse/src/wxParse.vue';
	import signBoard from '@/components/signBoard/signBoard.vue';
	import AliOss from "@/utils/aliOssSts.js";
	import THeader from '@/components/views/t-header/index.vue'; // 头部组件
	export default {
		components: {
			uParse,
			signBoard,
			THeader
		},
		data() {
			return {
				id: 0,
				con_info: {},
				moban: {},
				sign_img_temp: '',
				showSign: false,
				imgFlag:false,
				aliSTS:{}
			};
		},
		onLoad(t) {
			this.id = t.id
			this.getContact()
			this.getSTS()
		},
		methods: {
			clickNavBackFn() {
				const pages = getCurrentPages();
				if (pages.length >= 2) {
					uni.navigateBack();
				} else {
					uni.switchTab({
						url: '/pages/my/index'
					});
				}
			},
			getSTS(){
				this.http.post('/user/getAlicloudSTS')
				.then(res=>{
					// console.log(res)
					if(res.code == 1){
						let sts = res.data
						this.aliSTS = sts
					}
				})
			},
			onUploadTap(e) {
				let _this = this;
				let option = {
					savePath: "image",
					AccessKeySecret: _this.aliSTS.AccessKeySecret,
					SecurityToken: _this.aliSTS.SecurityToken,
					AccessKeyId: _this.aliSTS.AccessKeyId,
					bucket: _this.aliSTS.bucket,
					area: _this.aliSTS.RegionId,
				};
				// uni.showLoading()
				AliOss.upload(e, option, function (res) {
					if (res) {
						_this.sign_img_temp = res
						// uni.hideLoading()
					} else {
						// uni.hideLoading()
						uni.showToast({
							title:"图片上传失败，请稍后重试",
							icon:"none"
						})
					}
				});
				
			},
			close(value) {
				this.showSign = value
			},
			savesign(data) {
				this.sign_img_temp = data.tempFilePath
				// this.showSign = false;
				this.onUploadTap(data.tempFilePath)
			},
			getContact() {
				contractDetail({
						id: this.id
					})
					.then(res => {
						// console.log(res)
						if (res.code == 1) {
							this.con_info = res.data.info
							this.get_moban()
						}
					})
			},
			get_moban() {
				if (this.con_info.typedata == 1) {
					one().then(res => {
						// console.log(res)
						this.moban = res.data.info
					})
				} else {
					two().then(res => {
						// console.log(res)
						this.moban = res.data.info
					})
				}
			},
			clearSign() {
				this.sign_img_temp = ""
			},
			doSign(){
				if(!this.sign_img_temp){
					uni.showToast({
						title:"您还没有手写签名",
						icon:"none"
					})
					return false;
				}
				doSign({id:this.id,img:this.sign_img_temp})
				.then(res=>{
					console.log(res)
					if(res.code == 1){
						uni.showToast({
							title:"合同签署成功",
							icon:'none'
						})
						
						this.getContact()
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.contr-container {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;
		background: #F5F5F5;
		box-sizing: border-box;
	
		.contr-content {
			overflow-y: auto;
			flex: 1;
			display: flex;
			flex-direction: column;
			margin: 0 18rpx 26rpx;
			border-radius: 34rpx;
			background: #FFFFFF;
			
			.contr-content-tabs {
				display: flex;
				justify-content: center;
				align-items: center;
				height: 124rpx;
				padding: 0 52rpx;
				border-radius: 17px 17px 0 0;
				background: linear-gradient( 180deg, #FFFFFF 0%, #F7E9E9 100%);
				color: #000000;
				font-size: 48rpx;
				font-weight: 400;
				font-family: PingFangSC, PingFang SC;
			}
			
			.contr-content-comp {
				overflow-y: auto;
				flex: 1;
				position: relative;
			}
		}
	}
	
	.page {
		padding: 0 10px;
		background: #fff;

		.top-logo {
			background: #fff;
			width: 100%;
			position: absolute;
			top: 0;
			left: 0;
			display: flex;
			z-index: 2;
			justify-content: center;			
			box-shadow: 0px 1px 16px #ccc;

			image {
				max-height: 4rem;
			}
		}

		.tit {
			margin-top: 4rem;
			font-size: 22px;
			height: 40px;
			line-height: 40px;
			text-align: center;
			font-weight: bolder;
		}

		.sub-tit {
			font-size: 18px;
			height: 40px;
			line-height: 40px;
			text-align: center;
			color: #ff1600;
		}

		.ab-info {
			font-size: 14px;
			color: #333;
			font-weight: bold;

			.info-item {
				margin: 0.5rem 0;
			}
		}

		.sign-info {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			margin-bottom: 4rem;

			.sign-item {
				position: relative;
				width: 50%;

				.sign-pic {
					position: absolute;
					top: -1rem;
					left: 0;
					z-index: 2;

					image {
						width: 8rem;
						height: 8rem;
					}
				}

				.item-row {
					font-size: 12px;
					line-height: 20px;
					padding: 10px 0;
					display: flex;
					flex-direction: row;
					position: relative;

					image {
						position: absolute;
						top: -1rem;
						left: 5rem;
						max-width: 6rem;
					}
				}
			}
		}

		.sign-box {
			margin-bottom: 3rem;
			display: flex;
			flex-direction: column;
			align-items: center;

			.sign-box-box {
				border: 1px solid #333;
				border-radius: 5px;
				width: 86%;
				height: 12rem;
				
				.down-sign{
					    width: 100%;
					    font-size: 2rem;
					    font-weight: bold;
					    color: #eee;
					    line-height: 12rem;
					    text-align: center;
				}
			}

			.sign-box-buttons {
				margin-top: 2rem;
				display: flex;
				flex-direction: row;
				width: 100%;
				justify-content: space-around;
			}
		}
	}
</style>